package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.spring.AbstractAddress;
import model.spring.AddressList;
import model.spring.EmailonlyAddress;

public class EmailAddressView extends AbstractAddressView{
	
	private static final long serialVersionUID = -8858920570750325578L;
	
	private JTextField nameTextField;
	private JTextField emailaddressTextField;

	private EmailonlyAddress emailAddress;
	
	public EmailAddressView(AddressListView addressListView) {
		super(addressListView);
	}
			
	@Override
	protected AbstractAddress init() {
		this.setTitle("Address");
		this.setLayout(new BorderLayout());
		JPanel upperPanel = new JPanel();
		upperPanel.setBorder(BorderFactory.createTitledBorder("Addressinformationen"));
		upperPanel.setLayout(new GridLayout(2, 2, 5, 5));
		JLabel nameLabel = new JLabel("Name");
		nameTextField = new JTextField();
		JLabel emailaddressLabel = new JLabel("Emailadresse");
		emailaddressTextField = new JTextField();
		upperPanel.add(nameLabel);
		upperPanel.add(nameTextField);
		upperPanel.add(emailaddressLabel);
		upperPanel.add(emailaddressTextField);
		if(alv.getSelectedIndex() == -1) {
			emailAddress = new EmailonlyAddress();
		} else 
			emailAddress = editEmailAddress();
		this.add(upperPanel, BorderLayout.CENTER);
		return emailAddress;
	}

	private EmailonlyAddress editEmailAddress() {
		return (EmailonlyAddress) AddressList.getInstance().get(alv.getSelectedIndex());
	}

	@Override
	protected void populateFields() {
		nameTextField.setText(emailAddress.getName());
		emailaddressTextField.setText(emailAddress.getEmailaddress());
	}

	@Override
	protected void retrieveFields() {
		emailAddress.setName(nameTextField.getText());
		emailAddress.setEmailaddress(emailaddressTextField.getText());
	}

}
